package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:套餐业务层接口D的实现类
 */
@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 添加套餐
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //向套餐表插入数据
        setMealMapper.insert(setmeal);
        //获取生成的套餐id
        Long setMealId = setmeal.getId();

        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if (dishes != null && dishes.size() > 0) {
            dishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setMealId);
            });
        }
        /*for (SetmealDish dish : dishes) {
            dish.setSetmealId(setMealId);
        }*/
        //保存套餐和菜品的关联关系
        setMealDishMapper.batchAddDish(dishes);
    }

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setMealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Transactional
    public void batchRemove(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setMealMapper.getById(id);
            //起售中的套餐不能删除
            if (StatusConstant.ENABLE == setmeal.getStatus()) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        /*ids.forEach(setMealId -> {
            //删除套餐表中的数据
            setMealMapper.deleteById(setMealId);
            //删除套餐菜品关系表中的数据
            setMealDishMapper.deleteBySetMealId(setMealId);
        });*/

        //可以使用优化in优化
        //删除套餐表中的数据
        setMealMapper.batchDeleteById(ids);
        //删除套餐菜品关系表中的数据
        setMealDishMapper.batchDeleteBySetMealId(ids);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO getSetMealById(Long id) {
        //根据套餐ID查询套餐
        Setmeal setmeal = setMealMapper.getById(id);
        //根据套餐ID查询套餐对应的菜品
        List<SetmealDish> setmealDishes = setMealDishMapper.getSetMealDishById(id);

        SetmealVO setmealVO = new SetmealVO();
        //把查询到的套餐和对应的菜品集合添加到VO一并返回
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Transactional
    public void modify(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //1,修改套餐表，执行update
        setMealMapper.update(setmeal);
        //套餐id
        Long setmealId = setmealDTO.getId();
        //2,删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        setMealDishMapper.deleteBySetMealId(setmealId);
        //3,获取套餐对应菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //为套餐对应菜品设置套餐ID
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //4,重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        setMealDishMapper.batchAddDish(setmealDishes);
    }

    /**
     * 套餐的启售和停售
     *
     * @param status
     * @param id
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {
        //把值传给setMeal通过setMeal进行操作数据库
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.ENABLE) {
            //根据套餐id查询对应菜品
            List<Dish> dishList = setMealDishMapper.passSetmealIdGetDishInfo(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        //正常启售和停售
        setMealMapper.update(setmeal);
    }

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
